package com.authsystem.controller;

import com.authsystem.entity.Role;
import com.authsystem.entity.User;
import com.authsystem.service.RoleService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @GetMapping("/management")
    public String roleManagement(HttpSession session, Model model) {
        User user = (User) session.getAttribute("currentUser");
        if (user == null) {
            return "redirect:/";
        }

        model.addAttribute("currentUser", user);
        return "role-management";
    }

    @GetMapping("/detail")
    public String roleDetail(HttpSession session,
                             Model model,
                             @RequestParam Long roleId) {
        User user = (User) session.getAttribute("currentUser");
        if (user == null) {
            return "redirect:/";
        }

        Role role = roleService.getRoleWithMenusById(roleId);
        model.addAttribute("currentUser", user);
        model.addAttribute("role", role);

        return "role-detail";
    }
}